package com.zz.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.zz.dao.ConsumeLogsDao;
import com.zz.entity.ConsumeLogs;
import com.zz.entity.ConsumeLogsSearch;
import com.zz.entity.GetStatistics;
import com.zz.service.ConsumeLogsService;

@Service
public class ConsumeLogsImpl implements ConsumeLogsService {
	@Autowired
	ConsumeLogsDao cdao;
	
	
	public List<ConsumeLogs> GetAll1() {
		List<ConsumeLogs> clist = cdao.findAll();
		return clist; 
	}

	public Map<String, Object> GetAll(ConsumeLogsSearch conSearch, Integer page, Integer size) {
		if(page == 0) page=page+1;
		Pageable p = PageRequest.of(page-1, size);
		Specification<ConsumeLogs> spec = getSpec(conSearch);
		Page<ConsumeLogs> pagelist = cdao.findAll(spec,p);
		Integer count = (int) pagelist.getTotalElements();
		Map<String, Object>map=new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("data", pagelist.getContent());
		map.put("count", count);
		return map;
	}

	private Specification<ConsumeLogs> getSpec(ConsumeLogsSearch conSearch) {
		return new Specification<ConsumeLogs>() {
			
			private static final long serialVersionUID = 1L;

			public Predicate toPredicate(Root<ConsumeLogs> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				Predicate pre = criteriaBuilder.conjunction();
				List<Expression<Boolean>> list = pre.getExpressions();
				if(!StringUtils.isEmpty(conSearch.getCardNo())) {
					list.add(criteriaBuilder.equal(root.get("CardNo"), conSearch.getCardNo()));
				}
				if(!StringUtils.isEmpty(conSearch.getReadRoomId()))	{
					list.add(criteriaBuilder.equal(root.get("ReadRoomId"), conSearch.getReadRoomId()));
				}
				if(!StringUtils.isEmpty(conSearch.getStatus()))	{
					list.add(criteriaBuilder.equal(root.get("Status"), conSearch.getStatus()));
				}
				if(!StringUtils.isEmpty(conSearch.getFrontInTime()))	{
					list.add(criteriaBuilder.lessThanOrEqualTo(root.get("InTime"), conSearch.getFrontInTime()));
				}
				if(!StringUtils.isEmpty(conSearch.getAfterInTime()))	{
					list.add(criteriaBuilder.greaterThanOrEqualTo(root.get("InTime"), conSearch.getAfterInTime()));
				}
				if(!StringUtils.isEmpty(conSearch.getFrontOutTime()))	{
					list.add(criteriaBuilder.lessThanOrEqualTo(root.get("OutTime"), conSearch.getFrontOutTime()));
				}
				if(!StringUtils.isEmpty(conSearch.getAfterOutTime() ))	{
					list.add(criteriaBuilder.greaterThanOrEqualTo(root.get("OutTime"), conSearch.getFrontOutTime()));
				}
				return pre;
			}
		};
	}
	
	//测试分页查询
	public Page<ConsumeLogs> GetAll2(ConsumeLogsSearch conSearch, Integer page, Integer size) {
		Pageable p = PageRequest.of(page, size);
		Specification<ConsumeLogs> spec = getSpec(conSearch);
		return cdao.findAll(spec, p);
	}
	
	//查询数据总条数
	public Integer ConsumeLogsCount() {
		// TODO Auto-generated method stub
		return null;
	}

	//定时查询当日产生的访客数
	public List<GetStatistics> TimingConsumeLogs(String indate,String outdate) {
		return cdao.getTheDateCount(indate, outdate);
	}

	//添加打卡信息
	public Integer SaveAndFlush(ConsumeLogs con) {
		con.setInTime(new Date());
		con.setStatus("1");
		return cdao.saveAndFlushConsumeLogs(con);
	}

	

	

	
}
